<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据统计</el-breadcrumb-item>
      <el-breadcrumb-item>数据列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <div id="echarts"></div>

      <div slot="header">
        <span><!-- card title --></span>
      </div>
      <!-- card body -->
    </el-card>
  </div>
</template>




<script>
import jQuery from "jquery";
import _ from 'lodash';
import * as echarts from "echarts";
export default {
  data() {
    return {
      EchartsData: {},

        options: {
        title: {
          text: '用户来源'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#E9EEF3'
            }
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            boundaryGap: false
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ]
      }
    };
  },

  methods: {
    async getEchartsData() {
      const { data: res } = await this.$http.get(`reports/type/1`);
      console.log(res);
      this.EchartsData = _.merge(res.data,this.options);
    
      console.log(this.EchartsData);
    },

    darwEcharts() {
      //  var chart=echarts.init(jQuery('#echarts'))//Echarts只能用原生DOM获取标签
      console.log(this.EchartsData);
    
      var myChart = echarts.init(document.getElementById("echarts"), "dark");

      // 指定图表的配置项和数据
      /*      var option = {
            title: {
                text: 'ECharts 入门示例'
            },
            tooltip: {},
            legend: {
                data:['销量']
            },
            xAxis: {
                data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
            },
            yAxis: {},
            series: [{
                name: '销量',
                type: 'bar',
                data: [5, 20, 36, 10, 10, 20]
            }]
        };  */
  
      myChart.setOption(this.EchartsData);

      // 使用刚指定的配置项和数据显示图表。
      // myChart.setOption(this.EchartsData);
    },
  },

  created() {
    this.getEchartsData();
    console.log(this.EchartsData);
  },

  mounted() {

    //   坑：mounted
    // 在执行created然后发送请求的时候，mounted并不会等这次请求结束才去执行，而是直接就去执行，而此时data中的值还没有被赋值，所以拿不到
   
// 解决方案1，加延迟
   setTimeout(() => {
      console.log(this.EchartsData);
         this.darwEcharts();
    }, 100);

// 解决方案2，把http请求放在mounted里，而不是写在外面
 
  },
};
</script>


<style>
#echarts {
  width: 600px;
  height: 400px;
}
</style>
